﻿using System.Collections.Generic;
using TrueSync;

public class LogicSkillGroupComponent : LogicComponent
{
    LogicEntity mLEntity;
    Dictionary<int, SkillGroup> mSkills;//这个是技能组ID对应技能
    Dictionary<string, int> mInputToSkillDict;//这个是技能组的输入对应技能
    Dictionary<int, List<SkillStreamState>> mSkillStreamDict;//这个是缓存的技能流

    public SkillGroup CurrentSkillGroup {get; private set;}

    public int this[string inputIndex]
    {
        get
        {
            if (mInputToSkillDict.ContainsKey(inputIndex))
                return mInputToSkillDict[inputIndex];
            else
                return -1;
        }
    }

    public void Init(LogicEntity LEntity)
    {
        mLEntity = LEntity;
        mSkillStreamDict = new Dictionary<int, List<SkillStreamState>>();

        mSkills = new Dictionary<int, SkillGroup>();
        mInputToSkillDict = new Dictionary<string, int>();
    }

    public void Start()
    {
        //这里可以做技能的预加载

        mLEntity.GetLogicComponent<LogicInputComponent>().OnInputCallback += OnActionInput;
    }

    public void DoUpdate(FP deltaTime)
    {
        mLEntity.GetLogicComponent<LogicInputComponent>().OnInputCallback -= OnActionInput;
    }

    public void Destroy()
    {
        CurrentSkillGroup = null;
        mSkillStreamDict = null;
        mSkills = null;
    }

    public void UseSkillGroup(int skillGroupID, int skillStreamID = -1)
    {
        if (mSkills.ContainsKey(skillGroupID) == false)
            return;

        //应该加一个按键检测使用哪个技能
        SkillGroup selectedSkillGroup = mSkills[skillGroupID];
        if (selectedSkillGroup.CanUseSkill(mLEntity.self))
        {
            SkillStreamState initSkillStream = GetSkillStream(skillStreamID < 0 ? selectedSkillGroup.InitialSkillStreamID : skillStreamID);
            mLEntity.GetLogicComponent<LogicStateMachineComponent>().ChangeToSkillStream(initSkillStream);
            mLEntity.GetLogicComponent<LogicStateMachineComponent>().AllowSkillBreak = false;
        }
    }

    public void EnterNewSkillGroup(int skillGroupID)
    {
        SkillGroup skillGroup = mSkills[skillGroupID];
        //同一个技能组不要重复进入
        if (skillGroup == CurrentSkillGroup)
            return;

        //之前的技能组还没有完成先给完成
        if (CurrentSkillGroup != null)
        {
            SkillGroupFinish();
        }

        CurrentSkillGroup = mSkills[skillGroupID];
        //LogicMoveComponent moveComponent = mLEntity.GetLogicComponent<LogicMoveComponent>();
        //if (CurrentSkillGroup.UseLastStateMoveSpeed == false)
        //{
        //    moveComponent.MoveSpeed = TSVector2.zero;
        //}
        ////不继承上个技能的位置，需要问策划这个是什么意思
        //if (CurrentSkillGroup.UseLastStatePosition == false)
        //{
        //    moveComponent.Position = new TSVector2(moveComponent.Position.x, 0);
        //}
    }

    //正常打断和异常打断就应该回调到这里，还要处理异常打断
    public void SkillGroupFinish()
    {
        CurrentSkillGroup = null;
    }

    public SkillStreamState GetSkillStream(int id)
    {
        SkillStreamState skillStream = null;
        if (mSkillStreamDict.ContainsKey(id))
        {
            List<SkillStreamState> list = mSkillStreamDict[id];
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].IsFinished())
                {
                    skillStream = list[i];
                    break;
                }
            }
        }

        if (skillStream == null)
        {
            skillStream = new SkillStreamState();
            EByte info = new EByte();//TODO: 读信息
            skillStream.Deserialize(info);
        }

        return skillStream;
    }

    void OnActionInput(string inputString)
    {
        int skillGroupIndex = this[inputString];
        if(skillGroupIndex >= 0)
        {
            UseSkillGroup(skillGroupIndex);
        }
    }
}
